// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_GAME_STATUS, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import DataManager from "../manager/DataManager";
import SdkManager from "../manager/SdkManager";
import { privacyChannel } from "../Script/privacyChannel";
import { privacyChannelKS } from "../Script/privacyChannelKS";
import MenuLayer from "./MenuLayer";
import bh_toast from "../Script/bh_toast";
import MainLayer from "./MainLayer";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LoginRewardLayer extends BaseLayer {
    btnClose: cc.Node = null
    reward: cc.Node = null;;

    protected onLoad(): void {
        console.error("LoginRewardLayer")
        this.btnClose = cc.find('style/panel/close', this.node)
        this.btnClose.on('click', this.onCloseClick, this)

        this.reward = cc.find("style/panel/button", this.node)
        this.reward.on('click', this.getReward, this)
    }
    show() {

        if (!(privacyChannel.getChannel() as privacyChannelKS).isLoginReWard()) {

            return
        }
        super.show()
        // console.error("是不是来自于侧边栏", (privacyChannel.getChannel() as privacyChannelKS).isComeFromSliderBar())
        // console.error("是不是可以领取奖励", (privacyChannel.getChannel() as privacyChannelKS).isRewarded());
        // if ((privacyChannel.getChannel() as privacyChannelKS).isRewarded()) {
        //     this.siderBar.active = true
        // } else {
        //     this.siderBar.active = false
        // }

        // if ((privacyChannel.getChannel() as privacyChannelKS).channelObj.checkSliderBarIsAvailable()) {
        //     this.siderBar.active = true
        // } else {
        //     this.siderBar.active = false
        // }
    }
    onCloseClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.LOGINREWARD, false)


    }

    getReward() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        privacyChannel.getChannel().showVideo(() => {
            DataManager.instance.power += 10

            DataManager.instance.save();
            (privacyChannel.getChannel() as privacyChannelKS).reciveLoginReward()
            cc.resources.load("privacy/bh_toast", cc.Prefab, (error, prefab: cc.Prefab) => {
                if (error) {
                    console.error(error);
                    return;
                }
                let node = cc.instantiate(prefab);

                this.node.parent.getChildByName("MenuLayer").getComponent(MenuLayer).rendorPower()
                node.getComponent(bh_toast).show("获得：体力x10",
                    cc.director.getScene().getChildByName('Canvas').getChildByName("UI"))

            })

            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.LOGINREWARD, false)

        })
    }
}



